import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:osc/ui/widgets/web_scaffold.dart';
import 'package:url_launcher/url_launcher.dart';

import 'pd_util.dart';

class NavigatorUtil {

  static void pushPage(BuildContext context, Widget page, {String pageName}) {
    if (context == null || page == null/* || ObjectUtil.isEmpty(pageName)*/) return;
    Navigator.push(
        context, new CupertinoPageRoute<void>(builder: (ctx) => page));
  }

  static void pushPageReplacement(BuildContext context, Widget page) {
    if (context == null || page == null) return;
    Navigator.pushReplacement(
        context, new CupertinoPageRoute<void>(builder: (ctx) => page));
  }

  static Future<T> pushPageForResult<T>(BuildContext context, Widget page, {String pageName}) {
    if (context == null || page == null/* || ObjectUtil.isEmpty(pageName)*/) return null;
    return Navigator.push(
        context, new CupertinoPageRoute<T>(builder: (ctx) => page));
  }

  static void pushWeb(BuildContext context,
      {String title, String titleId, String url, bool isHome: false, bool autoAddPostfix = true}) {
    if (context == null || ObjectUtil.isEmpty(url)) return;
    if (url.endsWith(".apk")) {
      launchInBrowser(url, title: title ?? titleId);
    } else {
      Navigator.push(
          context,
          new CupertinoPageRoute<void>(
          builder: (ctx) => new WebScaffold(
        title: title,
        url: url,
            autoAddPostfix: autoAddPostfix,
      )));
    }
  }
  static Future<T> pushWebForResult<T>(BuildContext context,
      {String title, String url, bool isHome: false, bool autoAddPostfix = true}) {
    if (context == null || ObjectUtil.isEmpty(url)) return null;
    if (url.endsWith(".apk")) {
      return launchInBrowser(url, title: title ?? "");
    } else {
      return Navigator.push(
          context,
          new CupertinoPageRoute<T>(
              builder: (ctx) => new WebScaffold(
                title: title,
                url: url,
                autoAddPostfix: autoAddPostfix,
              )));
    }
  }

  static Future<T> pushForResult<T>(BuildContext context, {Widget page, String title, String titleId, String url}) {
    if (context == null || (page == null && (url == null || url.isEmpty))) return null;
    if(url.startsWith("http")){
      pushWeb(context, titleId: titleId, title: title, url: url);
      return null;
    } else if(url.startsWith("native")) {
      return pushPageForResult(context, page);
    } else {
      return null;
    }
  }

  static void pushUrl(BuildContext context, {String title, String titleId, String url}) {
    if (context == null || url == null || url.isEmpty) return null;
    if(url.startsWith("native")) {
      switch(url){
        case "":
          break;
      }
    } else if(url.startsWith("http") || url.startsWith("mqq://")){
      pushWeb(context, titleId: titleId, title: title, url: url);
    } else {
      pushWeb(context, titleId: titleId, title: title, url: PdUtil.getFullUrl(url));
    }
  }

  /*static void pushTabPage(BuildContext context,
      {String labelId, String title, String titleId, TreeModel treeModel}) {
    if (context == null) return;
    Navigator.push(
        context,
        new CupertinoPageRoute<void>(
        builder: (ctx) => new BlocProvider<TabBloc>(
      child: new TabPage(
        labelId: labelId,
        title: title,
        titleId: titleId,
        treeModel: treeModel,
      ),
      bloc: new TabBloc(),
    )));
  }*/

  static Future<T> launchInBrowser<T>(String url, {String title}) async {
    if (await canLaunch(url)) {
      await launch(url, forceSafariVC: false, forceWebView: false);
    } else {
      throw 'Could not launch $url';
    }
  }
}